package com.tsingtao.springboot2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsingtao.springboot2.dto.User;
import com.tsingtao.springboot2.mapper.UserMapper;
import com.tsingtao.springboot2.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> customSelectAll() {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq(User.Fields.id, "1");
        List<User> users = userMapper.customSelectList(userQueryWrapper);
        return users;
    }

    @Override
    public List<User> testQueryWrapper() {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.select(User.class, i -> !i.getColumn().equals("email"));
        List<User> list = this.list(userQueryWrapper);
        return list;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public List<User> testUpdateWrapper() {
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("id", 1);
        userUpdateWrapper.setSql("name = concat(name, name)");
        this.update(userUpdateWrapper);

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("id", "1");
        List<User> list = this.list(userQueryWrapper);
        return list;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public User testSave(User user) {
        this.save(user);
        return user;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public List<User> testBatchSave(List<User> userList) {
        this.saveBatch(userList, 2);
        return userList;
    }

    @Override
    public IPage<User> testPage(Page<User> page, User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        IPage<User> iPage = this.page(page, queryWrapper);
        log.info("分页查询： {}", iPage);
        return iPage;
    }


}
